package service

import (
	"perfect-admin/dao"
	"perfect-admin/model"
	"perfect-admin/model/request"
	"perfect-admin/model/response"
	"sort"
	"strings"
)

func GetApiTree() (api_tree_list []response.ApiTree) {
	var api_list []model.SysApi
	dao.DB.Find(&api_list)
	api_map := make(map[string][]response.ApiNode)
	var api_group_list []string
	for _, api := range api_list {
		OnlyId := "p:"+api.Path+"m:"+api.Method
		api_map[api.ApiGroup] = append(api_map[api.ApiGroup], response.ApiNode{Path:api.Path,Method:api.Method,Description:api.Description,ApiGroup:api.ApiGroup,OnlyId:OnlyId})
	}
	for api_group, _ := range api_map{
		api_group_list = append(api_group_list, api_group)
	}
	sort.Strings(api_group_list)
	for _, api_group := range api_group_list{
		api_tree_list = append(api_tree_list, response.ApiTree{ID:api_group,Description:api_group+"组",Children:api_map[api_group]})
	}
	return
}

func GetApiList(queryInfo request.QueryInfo) (apis []model.SysApi, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Find(&apis).Error
		return
	}
	if queryInfo.Query == "" && queryInfo.Method == ""{
		if err = dao.DB.Model(&model.SysApi{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&apis).Error
		return
	}
	if queryInfo.Method == "" {
		if err = dao.DB.Model(&model.SysApi{}).Where(queryInfo.KeyWord + " like ?", "%" + queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where(queryInfo.KeyWord + " like ?", "%" + queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&apis).Error
	}else if queryInfo.Query == "" {
		if err = dao.DB.Model(&model.SysApi{}).Where("method = ?", queryInfo.Method).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("method = ?", queryInfo.Method).Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&apis).Error
	}else {
		if err = dao.DB.Model(&model.SysApi{}).Where("method = ? and "+queryInfo.KeyWord + " like ?", queryInfo.Method, "%" + queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("method = ? and " + queryInfo.KeyWord + " like ?", queryInfo.Method, "%" + queryInfo.Query + "%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&apis).Error
	}
	return
}

func GetApiGroup() (api_group_list []string, err error) {
	var api_list []model.SysApi
	err = dao.DB.Model(&model.SysApi{}).Select("distinct(api_group)").Scan(&api_list).Error
	for _, api :=  range api_list {
		api_group_list = append(api_group_list, api.ApiGroup)
	}
	return
}

func AddApi(api model.SysApi) error {
	return dao.DB.Create(&api).Error
}

func UpdateApiInfo(api model.SysApi) error {
	return dao.DB.Model(&model.SysApi{}).Where("id = ?", api.ID).Updates(&model.SysApi{Path:api.Path,Description:api.Description,ApiGroup:api.ApiGroup,Method:api.Method}).Error
}

func DeleteApi(api_id uint) error {
	return dao.DB.Where("id = ?", api_id).Unscoped().Delete(&model.SysApi{}).Error
}

func GetApiByPathMethod(path string, method string) (api model.SysApi, err error)  {
	path = strings.Replace(path, "/api/v1", "", 1)
	err = dao.DB.Model(&model.SysApi{}).Where(model.SysApi{Path:path, Method:method}).First(&api).Error
	return
}